package leetcode.offer_two_special.stack;

import java.util.Stack;
import java.util.stream.Collectors;

/**
 * @author mazouri
 * @create 2021-09-10 17:56
 */
public class _37_asteroidCollision {
    public int[] asteroidCollision(int[] asteroids) {
        Stack<Integer> stack = new Stack<>();

        for (int as : asteroids) {
            while (!stack.isEmpty() && stack.peek() > 0 && stack.peek() < -as) stack.pop();

            if (!stack.isEmpty() && as < 0 && stack.peek() == -1) stack.pop();
            else if (as > 0 || stack.isEmpty() || stack.peek() < 0) stack.push(as);
        }

        return stack.stream().mapToInt(i -> i).toArray();
    }
}
